package service

import (
	JWT "chat_gin/common/middleware"
	"chat_gin/entity/dto"
	"chat_gin/entity/entity_user"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 这里用来做登录注册模块,其他的模块都是要走中间件鉴权的,登录注册不用鉴权

// Login 登录
func Login(c *gin.Context) {
	fmt.Println("请求参数", c.Params)
	param := dto.LoginInfo{}
	param.Name = "张艺谋"
	param.Pwd = "123456"
	loginRst := entity_user.LoginOpe(param)

	resultJson := gin.H{}
	token, err := JWT.GenerateToken(param.Name, param.Pwd)
	if err != nil {
		fmt.Println("获取token失败", err)
		loginRst = false
	} else {
		dataJson := gin.H{
			"token": token,
			"msg":   "登录成功",
		}

		resultJson = gin.H{
			"code": 200,
			"data": dataJson,
		}
	}
	if loginRst == false {
		resultJson = gin.H{
			"msg": "登录失败",
		}
	}
	c.JSON(http.StatusOK, resultJson)
}

// Reg 注册接口
func Reg(c *gin.Context) {
	param := dto.RegInfo{
		Name: c.Query("name"),
		Pwd:  c.Query("password"),
	}
	regRst := entity_user.RegOpe(param)
	resultJson := gin.H{
		"msg": "注册失败",
	}
	if regRst {
		resultJson = gin.H{
			"msg": "注册成功",
		}
	}
	c.JSON(http.StatusOK, resultJson)
}
